package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarriagetManageBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.CarMasterOderEntity;
import com.sgb.kjwl.view.ui.adapter.self.carMasters.TransportationOrderManagementAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.carMakers.TransportationOrderManagementModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;


public class TransportationOrderManagementActivity extends AppActivity {

    @Inject
    TransportationOrderManagementModel viewModel;
    ActivityCarriagetManageBinding binding;
    private TransportationOrderManagementAdapter adapter;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, TransportationOrderManagementActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_carriaget_manage);
        binding.include.titleView.setText("运输订单管理");
        binding.include.rightView.setVisibility(View.GONE);
        binding.include.rightView.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        if (adapter == null) {
            adapter = new TransportationOrderManagementAdapter(getActivity(), null);
        }
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.findCarOrderList(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.findCarOrderList(true);
            }
        });
        adapter.setOnItemClick(new AdapterOnItemClick<CarMasterOderEntity>() {
            @Override
            public void onItemClick(CarMasterOderEntity carMasterOderEntity, int position) {
                    viewModel.getH5Url(carMasterOderEntity.getOrderNo());
            }
        });
        adapter.onButtonClick(new TransportationOrderManagementAdapter.IButtonClick() {
            @Override
            public void fuckCar(String orderNo) {
                showDialog("发车确认", " 确定发车吗？", 0,orderNo);
            }

            @Override
            public void onCancel(String orderNo) {
                showDialog("取消确认", " 确定取消订单吗？？", 1,orderNo);
            }

            @Override
            public void onDelete(String orderNo) {
                showDialog("删除确认", " 确定删除吗？", 2,orderNo);
            }

            @Override
            public void onArrive(String orderNo) {
                showDialog("到达确认", " 确定到达吗？", 3,orderNo);
            }
        });
    }

    @Override
    protected void init() {
        super.init();
//        viewModel.myCarrierList(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.findCarOrderList(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    public void showList(List<CarMasterOderEntity> list) {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.loadMoreComplete();
        if (list.size() > 0) {
            adapter.setDatas(list);
            binding.emptyView.setVisibility(View.GONE);
        } else {
            adapter.setDatas(null);
            binding.emptyView.setVisibility(View.VISIBLE);
        }
        if (list.size() < 10) {
            binding.recyclerView.setLoadingMoreEnabled(false);
        } else {
            binding.recyclerView.setLoadingMoreEnabled(true);
        }
    }

    public void lodMore(List<CarMasterOderEntity> list) {
        binding.recyclerView.refreshComplete();
        if (list.size() > 0) {
            adapter.addDatas(list);
        }
        if (list.size() < 10) {
            binding.recyclerView.setLoadingMoreEnabled(false);
        } else {
            binding.recyclerView.setLoadingMoreEnabled(true);
        }
    }

    private void showDialog(String title, String content, int type,String orderNo) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText(title);
        tv_message.setText(content);

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求接口
                switch (type){
                    case 0:
                        viewModel.startCar(orderNo);
                        break;
                    case 1:
                        viewModel.cancel(orderNo);
                        break;
                    case 2:
                        viewModel.delOrder(orderNo);
                        break;
                    case 3:
                        viewModel.endCar(orderNo);
                        break;
                }
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
